/*
 * Copyright (C) 2009 Otávio Scherer Garcia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.technorati4j.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A commom utility methods for Strings.
 * 
 * @author Otávio Scherer Garcia
 * @version $Revision$
 */
public final class StringUtils {

    private static final Logger logger = LoggerFactory.getLogger(StringUtils.class);

    /**
     * Return true if the input String is not empty or not null, false otherwise.
     * 
     * @param str The input String.
     * @return true if the input String is not empty or not null, false otherwise.
     */
    public static boolean isNotEmpty(String str) {
        return str != null && str.length() > 0;
    }

    /**
     * Convert a String to Integer. If the parse fail or the input String is empty return a null object.
     * 
     * @param str The input String.
     * @return The value parsed, or null.
     */
    public static Integer toInteger(String str) {
        if (isNotEmpty(str)) {
            try {
                return new Integer(str.trim());
            } catch (NumberFormatException e) {
                // ignore if an error occurs
                logger.warn(e.getMessage());
            }
        }

        return null;
    }

    /**
     * Convert a String to Double. If the parse fail or the input String is empty return a null object.
     * 
     * @param str The input String.
     * @return The value parsed, or null.
     */
    public static Double toDouble(String str) {
        if (isNotEmpty(str)) {
            try {
                return new Double(str.trim());
            } catch (NumberFormatException e) {
                // ignore if an error occurs
                logger.warn(e.getMessage());
            }
        }

        return null;
    }

    /**
     * Convert a String to Date. If the parse fail or the input String is empty return a null object.
     * 
     * @param str The input String.
     * @param format The input String format.
     * @return The value parsed, or null.
     * @see SimpleDateFormat
     */
    public static Date toDate(String str, String format) {
        if (isNotEmpty(str)) {
            try {
                return new SimpleDateFormat(format).parse(str.trim());
            } catch (ParseException e) {
                // ignore if an error occurs
                logger.warn(e.getMessage());
            }
        }

        return null;
    }

}
